package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;

    @Autowired
    private IStatementService statementService;

    @Autowired
    private IAppointmentService appointmentService;

    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }


    @Override
    public void save(StatementItem statementItem) {
        statementItemMapper.insert(statementItem);
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(StatementItem statementItem) {
        statementItemMapper.updateByPrimaryKey(statementItem);
    }
    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public void saveItems(List<StatementItem> statementItems) {
        //先判断状态
        StatementItem temp = statementItems.remove(statementItems.size() - 1);
        BigDecimal discountAmount = temp.getItemPrice();
        Statement statement = statementService.get(temp.getStatementId());
        if(Statement.STATUS_PAID.equals(statement.getStatus())){
            throw new BusinessException("不能对已支付的结算单进行保持明细操作");
        }
        //删除之前的明细项
        statementItemMapper.deleteItemsByStatementId(statement.getId());

        BigDecimal totalAmount = new BigDecimal(0);
        BigDecimal totalQuantity = new BigDecimal(0);
        if(statementItems.size()>0){
            //说明有明细项
            for(StatementItem item:statementItems){
                statementItemMapper.insert(item);
                totalAmount = totalAmount.add(item.getItemPrice().multiply(item.getItemQuantity()));
                totalQuantity = totalQuantity.add(item.getItemQuantity());
            }
        }
        //如果优惠金额比总金额还要更多，属于非法逻辑
        if(totalAmount.compareTo(discountAmount)<0){
            throw new BusinessException("非法操作,优惠金额大于总金额");
        }

        statementService.updateAmount(statement.getId(),totalAmount,totalQuantity,discountAmount);
    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }

    @Override
    public void payStatement(Long statementId) {
        //从合理性判断
        Statement statement = statementService.get(statementId);
        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            throw new BusinessException("结算单已经支付,请不要重复支付");
        }
        //更新结算单状态
        statementService.updatePayStatus(statementId,Statement.STATUS_PAID, ShiroUtils.getUserId());
        if (statement.getAppointmentId() != null) {
            //说明该结算单是预约生成的
            //修改预约单状态
            appointmentService.changeStatus(statement.getAppointmentId(), Appointment.STATUS_SETTLE);
        }
    }
}
